package com.taomu.app.zongchouhuli.moudle.home.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.http.res.ResProductDetailInfoEntity;
import com.taomu.app.zongchouhuli.moudle.home.entity.ProductHistEntity;
import com.taomu.app.zongchouhuli.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class HomeProductHisAdp extends RecyclerView.Adapter<HomeProductHisAdp.HomeProductHisHolder>{

    private Context mContext;

    private LayoutInflater inflater;

    private List<ResProductDetailInfoEntity.BuyListBean> mData;

    public HomeProductHisAdp(Context mContext){
        this.mContext = mContext;
        this.mData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
    }

    public void setData(List<ResProductDetailInfoEntity.BuyListBean> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void addData(List<ResProductDetailInfoEntity.BuyListBean> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public HomeProductHisHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_product_detail_history, parent, false);
        return new HomeProductHisHolder(view);
    }

    @Override
    public void onBindViewHolder(HomeProductHisHolder holder, int position) {
        final ResProductDetailInfoEntity.BuyListBean entity = mData.get(position);
        holder.update(entity);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class HomeProductHisHolder extends RecyclerView.ViewHolder{

        private TextView mContentTv;

        private TextView mTimeTv;

        public HomeProductHisHolder(View itemView) {
            super(itemView);
            mContentTv = itemView.findViewById(R.id.item_product_detail_his_content_tv);
            mTimeTv = itemView.findViewById(R.id.item_product_detail_his_time_tv);
        }

        public void update(ResProductDetailInfoEntity.BuyListBean entity){
            mContentTv.setText(entity.getUsername());
            if(entity.getComplete_date() != 0) {
                mTimeTv.setText(TimeUtils.timeFomart(entity.getComplete_date()));
            }
        }
    }
}
